package com.zn.task;

import com.zn.service.MdInfoService;
import com.zn.socketReceiver.ReaderTask;
import com.zn.socketReceiver.SocketHandler;
import com.zn.util.AsyncTask;
import com.zn.util.Constant;
import com.zn.web.rpc.MdRemoteCall;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.LinkedList;


/**
 * Created by yinjiawei on 2018/7/12.
 * 监听socket是否正常的task
 */
@Component
public class ListenerSocketState {
    private static final Logger LOGGER = LoggerFactory.getLogger(ListenerSocketState.class);
    @Value("${md.socket.host}")
    private String host;
    @Value("${md.socket.port}")
    private String port;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MdInfoService mdInfoService;
    @Autowired
    private MdRemoteCall mdRemoteCall;
    @Autowired
    private AsyncTask asyncTask;

    @Scheduled(cron="*/5 * * * * ?")   //每5秒执行一次
    public void task(){
        try{
            SocketHandler socketHandler=SocketHandler.getInstance(host,Integer.valueOf(port),redisTemplate,mongoTemplate,mdInfoService,mdRemoteCall);
            boolean isClose=socketHandler.isServerClose();
            String isCloseStr=isClose==true?"关闭":"开启";
            LOGGER.warn("MD....socket是否关闭:" + isCloseStr);
            //如果是关闭的 需要重连
            if(isClose){
                sendStatus(1);
                SocketHandler.setEmpty();
                SocketHandler.getInstance(host,Integer.valueOf(port),redisTemplate,mongoTemplate,mdInfoService,mdRemoteCall);
            }else{
                sendStatus(0);
            }
        }catch (Exception e){
            sendStatus(1);
            LOGGER.error("MD...ListenerSocketState Error:" + e);
        }
    }

    private void sendStatus(Integer socketStatus){
        try {
            String uniqueIdentification=redisTemplate.opsForValue().get(Constant.BUSINESSUNIQUE);
            if(StringUtils.isNotBlank(uniqueIdentification)){
                mdRemoteCall.sendSocketStatus(uniqueIdentification, 1, socketStatus);
            }else{
                LOGGER.error("uniqueIdentification is null:" + uniqueIdentification);
            }
        }catch (Exception e){
            LOGGER.error("sendStatus......."+e);
        }

    }

    @Scheduled(cron="*/1 * 09-11 * * ?")
    public void getEtfMdInfo(){
        try {
            asyncTask.getEtfMdInfo();
        }catch (Exception ex){
            LOGGER.error("getEtfMdInfo:" + ex.getMessage());
        }
    }

    @Scheduled(cron="*/1 * 13-14 * * ?")
    public void getEtfMdInfo1(){
        try {
            asyncTask.getEtfMdInfo();
        }catch (Exception ex){
            LOGGER.error("getEtfMdInfo1:" + ex.getMessage());
        }
    }


}
